###################################################################
### Oil Theft and Violence in Mexico                           ####
### Figures and tables in the Supporting Information           ####                  
### This version: May 17, 2023                                 ####                   
###################################################################

#---------------------------------------------------#
# Preliminaries                                     #
#---------------------------------------------------#

rm(list = ls())

#List of packages for session
.packages = c("tidyverse", "stringi", "stringr",
              "estimatr", "patchwork", "modelsummary",
              "spdep", "sf", "MatchIt", "texreg")

#Install CRAN packages (if not already installed)
.inst = .packages %in% installed.packages()
if(length(.packages[!.inst]) > 0) install.packages(.packages[!.inst])

#Loading packages into session 
lapply(.packages, require, character.only=TRUE)

# Set Working Directory to wherever files are downloaded
#setwd("")

#---------------------------------------------------#
# Loading Data                                      #
#---------------------------------------------------#

#Main datasets for regression analysis and figures

##Master dataset at the locality level
master_locality <- read.csv("master_locality.csv") %>%
  dplyr::select(-1)

##Master dataset at the municipal level
master_municipality <- read.csv("master_municipality.csv") %>%
  dplyr::select(-1)

##############################################
### Supplementary Information -- Table S6 ####               
##############################################

#Table S6: Baseline model at the municipal level for different types of placebo outcomes

modmun_placebo1 <- felm(young_female_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

modmun_placebo2 <- felm(young_male_diabetes_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

texreg::texreg(list(modmun_placebo1, modmun_placebo2), digits = 6, include.ci = TRUE)

##############################################
### Supplementary Information -- Table S7 ####               
##############################################

#Table S7: Baseline model at the municipal level for different types of criminal outcomes

modmun_placebo3 <- felm(extortion_rate ~ fuel_pipeline*oil_price_usd|  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

modmun_placebo4 <- felm(kidnapping_rate ~ fuel_pipeline*oil_price_usd|  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

texreg::texreg(list(modmun_placebo3, modmun_placebo4), digits = 6, include.ci = TRUE)

##############################################
### Supplementary Information -- Table S8 ####               
##############################################

#Table S8: Energy Prices, Infrastructure, and Criminal Violence.

modmun_placebo5 <- felm(young_male_murder_rate ~ gas_pipeline_only*oil_price_usd|  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

modmun_placebo6 <- felm(young_male_murder_rate ~ oil_pipeline_only*oil_price_usd|  mun_id + year_month | 0 | mun_id, 
                        data = master_municipality)

texreg::texreg(list(modmun_placebo5, modmun_placebo6), digits = 6, include.ci = TRUE)

##############################################
### Supplementary Information -- Table S9 ####               
##############################################

#Table S9: Main Effects. War on Drugs

modmun1_main_war <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd | mun_id + year_month | 0 | mun_id, 
                         data = subset(master_municipality, year > 2005 & year < 2015))

modmun2_main_war <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                         data = subset(pipeline_vs_adjnopipe, year > 2005 & year < 2015))

modmun3_main_war <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                         data = subset(pipeline_vs_noadj_nopipe, year > 2005 & year < 2015))

modmun4_main_war <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                         data = subset(pipeline_vs_violent_nopipeline, year > 2005 & year < 2015) )

texreg::texreg(list(modmun1_main_war, modmun2_main_war, modmun3_main_war, modmun4_main_war), digits = 6, include.ci = TRUE)

##############################################
### Supplementary Information -- Table S10 ###               
##############################################

#Table S10: Main Effects. After 2000 Only

modmun1_main_2000 <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd | mun_id + year_month | 0 | mun_id, 
                          data = subset(master_municipality, year > 2000))

modmun2_main_2000 <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                          data = subset(pipeline_vs_adjnopipe, year > 2000))

modmun3_main_2000 <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                          data = subset(pipeline_vs_noadj_nopipe, year > 2000))

modmun4_main_2000 <- felm(young_male_murder_rate ~ fuel_pipeline*oil_price_usd |  mun_id + year_month | 0 | mun_id, 
                          data = subset(pipeline_vs_violent_nopipeline, year > 2000))

texreg::texreg(list(modmun1_main_2000, modmun2_main_2000, modmun3_main_2000, modmun4_main_2000), digits = 6, include.ci = TRUE)